#include "Login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);

    pixmap = new QPixmap("://1.jpg");
    *pixmap = pixmap->scaled(width(), height());

#ifdef Q_OS_ANDROID
    giv_radius = 180;   //圆直径
#else
    giv_radius = 60;    //圆直径
#endif
    giv_angleStep = 0;
    giv_currentValue = 0;
    giv_maxValue = 10;
    gcv_background = QColor(255, 255, 255);
    gcv_foreground = QColor(0, 0, 0);

    gtv_timer = new QTimer;
    gtv_timer->setInterval(150);    //定时器间隔

    giv_count = 0 ;
    connect(gtv_timer, &QTimer::timeout, [this](){
        this->update(); //刷新窗口
        ++giv_count;
        if(giv_count > 35)
        {
            gtv_timer->stop();
            w = new Widget;
            w->show();
            this->close();
        }
    });
    gtv_timer->start(); //启动定时器
}

Login::~Login()
{
    delete ui;
}

void Login::paintEvent(QPaintEvent *)
{
    QPainter lpv_painter(this);

    lpv_painter.drawPixmap(0, 0, width(), height(), *pixmap);
    //设置坐标(0,0)在窗口中心
    lpv_painter.translate(width() >> 1, height() >> 1);
    //设置画笔
    lpv_painter.setPen(gcv_foreground);
    //设置反走样
    lpv_painter.setRenderHint(QPainter::Antialiasing, true);
    //画阴阳鱼
    drawYu(&lpv_painter);
    //画八卦及文本
    drawGua(&lpv_painter);
}

void Login::drawYu(QPainter *painter)
{
    //设置旋转角度
    giv_angleStep -= 2;
    painter->rotate(giv_angleStep);

    //计算中心点坐标
    int liv_centerX = 0;
    int liv_centerY = 0;
    int liv_radiusBig = giv_radius / 2;
    int liv_radiusSmall = giv_radius / 6;
    double liv_currentangle = giv_currentValue * (360 / (giv_maxValue + 1));

    //绘制鱼身1
    painter->setBrush(gcv_foreground);
    QPainterPath pathBig1(QPointF(liv_centerX + giv_radius * qCos(degreesToRadians(liv_currentangle)),
                                  liv_centerY - giv_radius * qSin(degreesToRadians(liv_currentangle))));
    pathBig1.arcTo(liv_centerX - giv_radius, liv_centerY - giv_radius, giv_radius * 2, giv_radius * 2, liv_currentangle, 180);
    pathBig1.arcTo(liv_centerX + liv_radiusBig * qCos(degreesToRadians(liv_currentangle + 180)) - liv_radiusBig,
                   liv_centerY - liv_radiusBig * qSin(degreesToRadians(liv_currentangle + 180)) - liv_radiusBig,
                   liv_radiusBig * 2, liv_radiusBig * 2, liv_currentangle + 180, 180);
    pathBig1.arcTo(liv_centerX + liv_radiusBig * qCos(degreesToRadians(liv_currentangle)) - liv_radiusBig,
                   liv_centerY - liv_radiusBig * qSin(degreesToRadians(liv_currentangle)) - liv_radiusBig,
                   liv_radiusBig * 2, liv_radiusBig * 2, liv_currentangle + 180, -180
                   );
    painter->drawPath(pathBig1);

    //绘制鱼身2
    painter->setBrush(gcv_background);
    QPainterPath pathBig2(QPointF(liv_centerX + giv_radius * qCos(degreesToRadians(liv_currentangle)),
                                  liv_centerY - giv_radius * qSin(degreesToRadians(liv_currentangle))));
    pathBig2.arcTo(liv_centerX - giv_radius, liv_centerY - giv_radius, giv_radius * 2, giv_radius * 2, liv_currentangle, -180);
    pathBig2.arcTo(liv_centerX + liv_radiusBig * qCos(degreesToRadians(liv_currentangle + 180)) - liv_radiusBig,
                   liv_centerY - liv_radiusBig * qSin(degreesToRadians(liv_currentangle + 180)) - liv_radiusBig,
                   liv_radiusBig * 2, liv_radiusBig * 2, liv_currentangle + 180, 180);
    pathBig2.arcTo(liv_centerX + liv_radiusBig * qCos(degreesToRadians(liv_currentangle)) - liv_radiusBig,
                   liv_centerY - liv_radiusBig * qSin(degreesToRadians(liv_currentangle)) - liv_radiusBig,
                   liv_radiusBig * 2, liv_radiusBig * 2, liv_currentangle + 180, -180);
    painter->drawPath(pathBig2);

    //绘制鱼眼1
    painter->drawEllipse(QRectF(liv_centerX + liv_radiusBig * qCos(degreesToRadians(180 + liv_currentangle)) - liv_radiusSmall,
                                liv_centerY - liv_radiusBig * qSin(degreesToRadians(180 + liv_currentangle)) - liv_radiusSmall,
                                liv_radiusSmall * 2, liv_radiusSmall * 2));
    //绘制鱼眼2
    painter->setBrush(gcv_foreground);
    painter->drawEllipse(QRectF(liv_centerX + liv_radiusBig * qCos(degreesToRadians(liv_currentangle)) - liv_radiusSmall,
                                liv_centerY - liv_radiusBig * qSin(degreesToRadians(liv_currentangle)) - liv_radiusSmall,
                                liv_radiusSmall * 2, liv_radiusSmall * 2));
}
double Login::degreesToRadians(double degrees)
{
    return degrees * (M_PI / 180);
}

void Login::drawGua(QPainter *painter)
{
    painter->rotate(-giv_angleStep * 2);      //相反反方向旋转相同角度
    painter->setBrush(gcv_foreground);        //设置画刷

    int liv_xy1 = int(-giv_radius * 0.4);     //-40
    int liv_xy2 = int(giv_radius * 0.02);     //2
    int liv_lD = int(giv_radius * 0.8);       //80
    int liv_ld = int(giv_radius * 0.38);      //38
    int liv_width = int(giv_radius * 0.2);    //20
    int liv_yx1 = int(giv_radius * 1.6);      //160
    int liv_yx2 = int(giv_radius * 1.35);     //135
    int liv_yx3 = int(giv_radius * 1.1);      //110
    int liv_yx4 = int(-giv_radius * 1.8);      //-180
    int liv_yx5 = int(-giv_radius * 1.55);     //-155
    int liv_yx6 = int(-giv_radius * 1.3);      //-130

    int liv_tx = int(-giv_radius * 0.4);       //-40
    int liv_ty = int(liv_width * 10.5);        //210

#ifdef Q_OS_ANDROID
    painter->setFont(QFont("微软雅黑", 15));   //设置字体
#else
    painter->setFont(QFont("微软雅黑", 12));   //设置字体
#endif

    //乾三连
    painter->drawRect(liv_xy1, liv_yx1, liv_lD, liv_width);
    painter->drawRect(liv_xy1, liv_yx2, liv_lD, liv_width);
    painter->drawRect(liv_xy1, liv_yx3, liv_lD, liv_width);
    //坤六断
    painter->drawRect(liv_xy1, liv_yx4, liv_ld, liv_width);
    painter->drawRect(liv_xy1, liv_yx5, liv_ld, liv_width);
    painter->drawRect(liv_xy1, liv_yx6, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx4, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx5, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx6, liv_ld, liv_width);
    //离中虚
    painter->drawRect(liv_yx4, liv_xy1, liv_width, liv_lD);
    painter->drawRect(liv_yx5, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx5, liv_xy2, liv_width, liv_ld);
    painter->drawRect(liv_yx6, liv_xy1, liv_width, liv_lD);
    //坎中满
    painter->drawRect(liv_yx1, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx1, liv_xy2, liv_width, liv_ld);
    painter->drawRect(liv_yx2, liv_xy1, liv_width, liv_lD);
    painter->drawRect(liv_yx3, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx3, liv_xy2, liv_width, liv_ld);
    //旋转45度
    painter->rotate(45);
    //兑上缺
    painter->drawRect(liv_xy1, liv_yx1, liv_lD, liv_width);
    painter->drawRect(liv_xy1, liv_yx2, liv_lD, liv_width);
    painter->drawRect(liv_xy1, liv_yx3, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx3, liv_ld, liv_width);
    //艮覆碗
    painter->drawRect(liv_xy1, liv_yx4, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx4, liv_ld, liv_width);
    painter->drawRect(liv_xy1, liv_yx5, liv_ld, liv_width);
    painter->drawRect(liv_xy2, liv_yx5, liv_ld, liv_width);
    painter->drawRect(liv_xy1, liv_yx6, liv_lD, liv_width);
    //震仰盂
    painter->drawRect(liv_yx4, liv_xy1, liv_width, liv_lD);
    painter->drawRect(liv_yx5, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx5, liv_xy2, liv_width, liv_ld);
    painter->drawRect(liv_yx6, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx6, liv_xy2, liv_width, liv_ld);
    //巽下断
    painter->drawRect(liv_yx1, liv_xy1, liv_width, liv_ld);
    painter->drawRect(liv_yx1, liv_xy2, liv_width, liv_ld);
    painter->drawRect(liv_yx2, liv_xy1, liv_width, liv_lD);
    painter->drawRect(liv_yx3, liv_xy1, liv_width, liv_lD);

    QStringList slist;
    slist << "乾三连" << "兑上缺" << "离中虚" << "震仰盂" << "坤六断" << "艮覆碗"  << "坎中满"  << "巽下断" ;
    painter->rotate(-90);
    for (int i = 0; i < 8; ++i)
    {
        //旋转45度
        painter->rotate(45);
        painter->drawText(liv_tx, liv_ty, slist.at(i));
    }
}
